/**
 * Created by own on 2017/12/9.
 */
//获取地址参数
function addressDl() {
    var urlInfo = decodeURI(window.location.href);
    var offset = urlInfo.indexOf('?');
    var newsInfo = urlInfo.substr(offset + 1, urlInfo.length);
    var addressDls = newsInfo.split('=');
    return addressDls;
}
var addressDls = addressDl();
var html = '';
var page = 1;
var addressDls = addressDl();
var urlType = addressDls[0];
var urlName = addressDls[1];
var urlname2='';
//动态添加的Input组件需要重新进行初始化
$(function () {
    switch(urlName){
        case '0':
            urlname2='京剧';break;
        case '1':
            urlname2='评剧';break;
        case '2':
            urlname2='梆子';break;
        case '3':
            urlname2='豫剧';break;
        case '6':
            urlname2='越剧';break;
    }
    $('header section').html('所在位置:<a href="index.html">首页</a>&gt;' + urlname2);
    $('title').text(urlname2);
});
//关注
function guanzhu(id) {
    $.ajax({
        url: url + 'guanzhu',
        dataType: 'json',
        type: 'post',
        async: true,
        data: {
            'teachid': id
        },
        success: function (result) {
            // console.log(result);
            var status = result.status;
            var code = result.code;
            if (status == 1 && code == '1000') {
                mui.alert('关注成功', function () {
                });
            } else if (status == 1 && code == '1002') {
                mui.alert('未登录', function () {
                });
                // window.history.back();
            } else if (status == 1 && code == '1008') {
                mui.alert('已经关注过', function () {
                });
            }
        },
        error: function (result) {
            mui.alert(result, function () {
            });
            ;
        }
    })
}
$(function () {

    //加载更多
    var pageSize = 15;//每页显示条数
    var page = 1;//计数器
    var pageStart = 0;//开始数据条数
    var Flag = true;
  mui.init({
        pullRefresh:{
            container:"#teacher",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
            up : {
                height:50,//可选.默认50.触发上拉加载拖动距离
                auto:false,//可选,默认false.自动上拉加载一次
                contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
                contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
                callback : function(){
                    this.endPullupToRefresh(Flag); //参数为true代表没有更多数据了。
                    load();
                  

                } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            }
        }
    });
    load();
    function load(pageStart,pageSize) {
        var html="";
        $.ajax({
            url: url + 'searchteachers?' + urlType + '=' + urlName,
            type: 'post',
            dataType: 'json',
            data: {page: page},
            async: 'true',
            timeout: 2000,
            success: function (data) {

                if (data.code == 1000 && data.status == 1) {
                    var indexLt = data.data;
                    Flag = indexLt == null || indexLt == undefined || indexLt == '';
                    //判断是否有返回值 当没有返回值的时候就为空，则代表没有更多数据了
                    console.log(Flag);
                    console.log(indexLt);
                    if(Flag==false){
                        page++;
                    }
                    console.log(page);
                    $.each(indexLt, function (k, v) {
                        this.xingbie == 1 ? this.xingbie = 'flag1' : this.xingbie = 'nflag1';
                        this.isrenzheng == 1 ? this.isrenzheng = '<i class="flag2"></i>' : this.isrenzheng = '';

                        
                html += '<li>'
                    + '<div class="te_left">'
           
                   +'<a href="detail.html?id='+this.id+'">'
                            + '<img src="' + this.headimage + '"/></a>'
                    + '</div>'
                    + '<div class="te_right">'
                    + '<h5><span>' + this.xingming + '</span><i class="' + this.xingbie + '"></i>' + this.isrenzheng
                    +'<button class="attention mui-pull-right"><a onclick="guanzhu('+this.id+')">关注</a></button>'
                    + '</h5>'
                    + '<p>' + this.hangdang + '</p>'
                    + '<div class="te_bottom">'
                    + '<button><a href="detail.html?id=' + this.id + '">立即预约</a></button>'
                    + '<p>'
                    + '<span class="color1">￥</span>'
                    + '<span class="color1 font16">' + this.price + '</span>'
                    + '<span class="color2">起</span>'
                    + '</p>'
                   +'</div>'
                    + '</div>'
                + '</li>';
                        if (this.stdate != null && this.stdate != '') {
                            var week = '';
                            switch (this.week) {
                                case '1':
                                    week = '周一';
                                    break;
                                case '2':
                                    week = '周二';
                                    break;
                                case '3':
                                    week = '周三';
                                    break;
                                case '4':
                                    week = '周四';
                                    break;
                                case '5':
                                    week = '周五';
                                    break;
                                case '6':
                                    week = '周六';
                                    break;
                                case '7':
                                    week = '周日';
                                    break;
                            }
                            html += '<p class="mui-text-center">'
                                + '<b>预约时间:</b>'
                                + '日期：' + this.stdate + '/' + this.enddate + ' ' + week + '<br><b>时间段：</b>' + this.sttime + '/' + this.endtime
                                + '</p>';

                        }



                    });
                    $(html).appendTo('#teacher .mui-scroll .mui-table-view');
                    mui('#teacher').pullRefresh().endPulldownToRefresh();//WJ
                    //上拉加载数据
  
                   // mui('body').on('tap', 'a', function () { document.location.href = this.href; });
                }else if(page==1){
                      html ='<li>没有数据</li>';
                    $('#teacher .mui-scroll .mui-table-view').html(html);
                    mui('#teacher').pullRefresh().endPulldownToRefresh();//WJ             
                   }
            },
            error: function (data) {
                mui.alert('数据加载失败', function() {});
            }
        })

    }
    
});










